#include "reportweather.h"
#include "ui_reportweather.h"

#include <QMouseEvent>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QMessageBox>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QPainter>
#include "terminalconsole.h"


reportWeather::reportWeather(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::reportWeather)
{
    ui->setupUi(this);
    //创建一个网络通道管理对象 当前窗口为父窗口
    manager = new QNetworkAccessManager(this);
    //连接网络对象
    connect(manager,SIGNAL(finished(QNetworkReply *)),this,SLOT(finishedSlot(QNetworkReply *)));
    //发送网络请求
    manager->get(QNetworkRequest(QUrl("http://t.weather.sojson.com/api/weather/city/101010100")));

    //连接按钮的槽函数
    connect(ui->toolButton,SIGNAL(clicked()),this,SLOT(on_toolButton_clicked()));
    connect(ui->toolButton_2,SIGNAL(clicked()),this,SLOT(on_toolButton_2_clicked()));
    connect(ui->toolButton_3,SIGNAL(clicked()),this,SLOT(on_toolButton_3_clicked()));
    //去窗口边框
    setWindowFlags(Qt::FramelessWindowHint | windowFlags());
    //把窗口背景设置为透明;
    setAttribute(Qt::WA_TranslucentBackground);

    mWeekList << ui->labelday0 << ui->labelday1
              << ui->labelday2 << ui->labelday3
              << ui->labelday4 << ui->labelday5;
    mDateList << ui->labelDate0 << ui->labelDate1
              << ui->labelDate2 << ui->labelDate3
              << ui->labelDate4 << ui->labelDate5;
    mIconList << ui->labelWheatherIcon0 << ui->labelWheatherIcon1
              << ui->labelWheatherIcon2 << ui->labelWheatherIcon3
              << ui->labelWheatherIcon4 << ui->labelWheatherIcon5;
    mWeaTypeList << ui->lbweatherTypeDate0 << ui->lbweatherTypeDate1
                 << ui->lbweatherTypeDate2 << ui->lbweatherTypeDate3
                 << ui->lbweatherTypeDate4 << ui->lbweatherTypeDate5;
    mAirqList << ui->labelaiq0 << ui->labelaiq1
              << ui->labelaiq2 << ui->labelaiq3
              << ui->labelaiq4 << ui->labelaiq5;
    mFxList << ui->labelFX0 << ui->labelFX1
            << ui->labelFX2 << ui->labelFX3
            << ui->labelFX4 << ui->labelFX5;
    mFlList << ui->labelFL0 << ui->labelFL1
            << ui->labelFL2 << ui->labelFL3
            << ui->labelFL4 << ui->labelFL5;

    //根据keys,设置icon的路径
    mTypeMap.insert("暴雪",":/res/type/BaoXue.png");
    mTypeMap.insert("暴雨",":/res/type/BaoYu. png");
    mTypeMap.insert("暴雨到大暴雨",":/res/type/BaoYuDaoDaBaoYu.png");
    mTypeMap.insert("大暴雨",":/res/type/DaBaoYu.png");
    mTypeMap.insert("大暴雨到特大暴雨",":/res/type/DaBaoYuDaoTeDaBaoYu.png");
    mTypeMap.insert("大到暴雪",":/res/type/DaDaoBaoXue.png");
    mTypeMap.insert("大雪",":/res/type/DaXue.png");
    mTypeMap.insert("大雨",":/res/type/DaYu.png");
    mTypeMap.insert("冻雨",":/res/type/DongYu.png");
    mTypeMap.insert("多云",":/res/type/DuoYun.png");
    mTypeMap.insert("浮沉",":/res/type/FuChen.png");
    mTypeMap.insert("雷阵雨",":/res/type/LeiZhenYu.png");
    mTypeMap.insert("雷阵雨伴有冰雹",":/res/type/LeiZhenYuBanYouBingBao.png");
    mTypeMap.insert("霾",":/res/type/Mai.png");
    mTypeMap.insert("强沙尘暴",":/res/type/QiangShaChenBao.png");
    mTypeMap.insert("晴",":/res/type/Qing.png");
    mTypeMap.insert("沙尘暴",":/res/type/ShaChenBao.png");
    mTypeMap.insert("特大暴雨",":/res/type/TeDaBaoYu.png");
    mTypeMap.insert("undefined",":/res/type/undefined.png");
    mTypeMap.insert("雾",":/res/type/Wu.png");
    mTypeMap.insert("小到中雪",":/res/type/XiaoDaoZhongXue.png");
    mTypeMap.insert("小到中雨",":/res/type/XiaoDaoZhongYu.png");
    mTypeMap.insert("小雪",":/res/type/XiaoXue.png");
    mTypeMap.insert("小雨",":/res/type/XiaoYu.png");
    mTypeMap.insert("雪",":/res/type/Xue.png");
    mTypeMap.insert("扬沙",":/res/type/YangSha.png");
    mTypeMap.insert("阴",":/res/type/Yin.png");
    mTypeMap.insert("雨",":/res/type/Yu.png");
    mTypeMap.insert("雨夹雪",":/res/type/YuJiaXue.png");
    mTypeMap.insert("阵雪",":/res/type/ZhenXue.png");
    mTypeMap.insert("阵雨",":/res/type/ZhenYu.png");
    mTypeMap.insert("中到大雪",":/res/type/ZhongDaoDaXue.png");
    mTypeMap.insert("中到大雨",":/res/type/ZhongDaoDaYu.png");
    mTypeMap.insert("中雪",":/res/type/ZhongXue.png");
    mTypeMap.insert("中雨",":/res/type/ZhongYu.png");

}

void reportWeather::parseJsonDocument(QByteArray byteArray)    //解析JsonDocument文档函数
{
    //将网络读取的字节数组转换为JsonDocument文档
    pJsonDocument = QJsonDocument::fromJson(byteArray);
    //将文档转为一个对象
    QJsonObject pJsonDocumentObject = pJsonDocument.object();
    //需要获取的数据
    QString place;//预报的地点
    QString date;//预报的日期
    QString humidness;//当天湿度
    QString airQuality;//空气质量
    QString averTem;//平均温度
    QString highTem;//最高温度
    QString lowTem;//最低温度
    QString weatherType;//天气类型
    QString windPower;//风力等级
    QString whatDay;//星期几
    QString pm25;//pm25指数
    QString ganMao;//感冒指数

    /*获取数据*/
    //获取预报地点
    QJsonValue valuePlace = pJsonDocumentObject.value("cityInfo");
    QJsonObject placeObject = valuePlace.toObject();
    valuePlace = placeObject.value("city");
    place = valuePlace.toString();
    //将解析的地址显示
    ui->labelCity->setText(place);

    //将键值为data的内容转为一个对象
    QJsonValue valueData = pJsonDocumentObject.value("data");
    QJsonObject valueDataObject = valueData.toObject();

    //获取当前湿度
    QJsonValue valueDataObjectShidu = valueDataObject.value("shidu");
    humidness = valueDataObjectShidu.toString();
    //将解析的湿度显示
    ui->labelShiduData->setText(humidness);

    //获取当前空气质量
    QJsonValue valueDataObjectQuality = valueDataObject.value("quality");
    airQuality = valueDataObjectQuality.toString();
    //将解析的空气质量显示
    ui->labelairData->setText(airQuality);

    //获取当前PM25
    int pmIntVal = valueDataObject.value("pm25").toInt();
    ui->labelPM25Data->setText(QString("%1").arg(pmIntVal));
    //获取平均温度
    QJsonValue valueDataObjectWendu = valueDataObject.value("wendu");
    averTem = valueDataObjectWendu.toString();
    //将解析的温度显示
    ui->labelTmp->setText(averTem+"℃");

    //获取感冒指数
    QJsonValue valueDataObjectGanMao = valueDataObject.value("ganmao");
    ganMao = valueDataObjectGanMao.toString();
    //显示感冒指数
    ui->labelGaomao->setText(ganMao);

    /* 获取一周内的天气数据*/
    QJsonValue valueDataObjectforecast = valueDataObject.value("forecast");
    QJsonArray forecastArr = valueDataObjectforecast.toArray();
    QJsonArray::iterator it_1;//定义一个指向forecastArr的迭代器
    it_1=forecastArr.begin();
    QJsonObject forecastArrObject = (*it_1).toObject();
    //获取预报 日期 与 星期
    QJsonValue value = forecastArrObject.value("ymd");
    date = value.toString();
    value = forecastArrObject.value("week");
    whatDay = value.toString();
    //将解析的日期 星期显示
    ui->labelCurrentDate->setText(date+" "+ whatDay);
    //获取预报的最高温度与最低温度
    value = forecastArrObject.value("high");
    highTem = value.toString();
    value = forecastArrObject.value("low");
    lowTem = value.toString();
    //将解析的最高温 最低温显示
    ui->labelTempRange->setText(lowTem+"~"+highTem);
    //获取天气类型
    value = forecastArrObject.value("type");
    weatherType = value.toString();
    ui->labelweatherType->setText(weatherType+"天");
    ui->labelWeatherIcon->setPixmap(mTypeMap.find(weatherType).value());
    //设置风力风向
    ui->labelFXType->setText(forecastArrObject.value("fx").toString());
    ui->labelFXType_2->setText(forecastArrObject.value("fl").toString());
    for (int i=0;i<7;i++)
    {
        days[i].mTempLow = forecastArr[i].toObject().value("low").toString();
        days[i].mTempHigh = forecastArr[i].toObject().value("high").toString();
        days[i].mDate = forecastArr[i].toObject().value("date").toString();
        days[i].mWeathType = forecastArr[i].toObject().value("type").toString();
        days[i].mFx = forecastArr[i].toObject().value("fx").toString();
        days[i].mFl = forecastArr[i].toObject().value("fl").toString();
        days[i].mAirq = forecastArr[i].toObject().value("aqi").toInt();
        days[i].mWeek = forecastArr[i].toObject().value("week").toString();
    }
    //五天内风力等级及风力
    ui->labelFX0->setText(days[0].mFx);
    ui->labelFL0->setText(days[0].mFl);
    ui->labelFX1->setText(days[1].mFx);
    ui->labelFL1->setText(days[1].mFl);
    ui->labelFX2->setText(days[2].mFx);
    ui->labelFL2->setText(days[2].mFl);
    ui->labelFX3->setText(days[3].mFx);
    ui->labelFL3->setText(days[3].mFl);
    ui->labelFX4->setText(days[4].mFx);
    ui->labelFL4->setText(days[4].mFl);
    ui->labelFX5->setText(days[5].mFx);
    ui->labelFL5->setText(days[5].mFl);
    //更新空气质量背景
    for(int i=0;i<=5;i++)
    {
        //更新空气质量
        if (days[i].mAirq >= 0 && days[i].mAirq <= 50)
        {
             mAirqList[i]->setText("优");
             mAirqList[i]->setStyleSheet("background-color: rgb(121, 184, 0);");
        }
        else if ( days[i].mAirq > 50 &&  days[i].mAirq <= 100)
        {
             mAirqList[i]->setText("良");
             mAirqList[i]->setStyleSheet("background-color: rgb(255, 187, 23);");
        }
        else if (days[i].mAirq > 100 && days[i].mAirq <= 150)
        {
             mAirqList[i]->setText("轻度");
             mAirqList[i]->setStyleSheet("background-color: rgb(255, 87, 97);");
        }
        else if (days[i].mAirq > 150 && days[i].mAirq <= 200)
        {
             mAirqList[i]->setText("中度");
             mAirqList[i]->setStyleSheet("background-color: rgb(235, 17, 27);");
        }
        else if (days[i].mAirq > 200 && days[i].mAirq <= 250)
        {
             mAirqList[i]->setText("重度");
             mAirqList[i]->setStyleSheet("background-color: rgb(170, 0, 0);");
        }
        else
        {
             mAirqList[i]->setText("严重");
             mAirqList[i]->setStyleSheet("background-color: rgb(110, 0, 0);");
        }
    }
    //六天内天气类型
    ui->lbweatherTypeDate0->setText(days[0].mWeathType);
    ui->labelWheatherIcon0->setPixmap(mTypeMap.find(days[0].mWeathType).value());
    ui->labelWheatherIcon0->setScaledContents(true);
    ui->lbweatherTypeDate1->setText(days[1].mWeathType);
    ui->labelWheatherIcon1->setPixmap(mTypeMap.find(days[1].mWeathType).value());
    ui->labelWheatherIcon1->setScaledContents(true);
    ui->lbweatherTypeDate2->setText(days[2].mWeathType);
    ui->labelWheatherIcon2->setPixmap(mTypeMap.find(days[2].mWeathType).value());
    ui->labelWheatherIcon2->setScaledContents(true);
    ui->lbweatherTypeDate3->setText(days[3].mWeathType);
    ui->labelWheatherIcon3->setPixmap(mTypeMap.find(days[3].mWeathType).value());
    ui->labelWheatherIcon3->setScaledContents(true);
    ui->lbweatherTypeDate4->setText(days[4].mWeathType);
    ui->labelWheatherIcon4->setPixmap(mTypeMap.find(days[4].mWeathType).value());
    ui->labelWheatherIcon4->setScaledContents(true);
    ui->lbweatherTypeDate5->setText(days[5].mWeathType);
    ui->labelWheatherIcon5->setPixmap(mTypeMap.find(days[5].mWeathType).value());
    ui->labelWheatherIcon5->setScaledContents(true);
    //六天内日期
     for(int i=0;i<=5;i++)
     {
         mDateList[i]->setText("10/"+days[i].mDate);
     }
     //六天内星期
     for(int i=0;i<=5;i++)
     {
         mWeekList[i]->setText(days[i].mWeek);
     }
}

void reportWeather::mouseMoveEvent(QMouseEvent *e)
{
    if (e->buttons() & Qt::LeftButton) {
               this->move(this->x() + e->globalX() - p.x(), this->y() + e->globalY() - p.y());
               p = e->globalPos();
           }
}

void reportWeather::mousePressEvent(QMouseEvent *e)
{
    if (e->button() == Qt::LeftButton) {
               p = e->globalPos();
    }
}

void reportWeather::finishedSlot(QNetworkReply *reply)
{
    //QNetworkRequest::HttpStatusCodeAttribute网址返回的状态码
    if(reply->attribute(QNetworkRequest::HttpStatusCodeAttribute) == 200)
    {
        qDebug()<<"访问服务器成功"<<endl;
        //访问成功 将访问的内容传回 给解析函数
        parseJsonDocument(reply->readAll());
    }
}

reportWeather::~reportWeather()
{
    delete ui;
}


void reportWeather::on_pushButton_clicked()
{
    QString cityNameFromUser = ui->lineEditCity->text();
    QString cityCode = cityCodeUtils.getCityCodeFromName(cityNameFromUser);
    if(cityCode != NULL){
        //发送网络请求
        manager->get(QNetworkRequest(QUrl(QString("http://t.weather.sojson.com/api/weather/city/%1").arg(cityCode))));
    }else{
        QMessageBox mes;
        mes.setWindowTitle("错误");
        mes.setText("请输入正确的城市名");
        mes.setStyleSheet("QPushButton {color:red}");
        mes.setStandardButtons(QMessageBox::Ok);
        mes.exec();
    }
}

void reportWeather::on_toolButton_clicked()
{
    showMinimized();
}

void reportWeather::on_toolButton_2_clicked()
{
    TerminalConsole * ter = new TerminalConsole;
    ter->show();
    delete this;
}

void reportWeather::on_toolButton_3_clicked()
{
    close();
}
